package com.ymatou.iapi.payment.testcase.old;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.payment.parameter.*;
import com.ymatou.iapi.payment.service.*;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.PaymentProxyCallService;
import com.ymttest.database.model.PPBussinessOrder;
import com.ymttest.database.model.PPPayment;
import com.ymttest.database.model.RefundRequest;
import com.ymttest.database.sqlwapper.PaymentDefaultWapper;
import com.ymttest.utils.YMTDateUtil;

/**
 * 系统自动审核退款 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_QueryByRefundNo {
	private static QueryByRefundNoBean QueryByRefundNoBean;
	private static QueryByRefundNoCall QueryByRefundNoCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询退款");
	}

	@Before
	public void caseUp() {
		QueryByRefundNoBean = new QueryByRefundNoBean();
		QueryByRefundNoCall = new QueryByRefundNoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(QueryByRefundNoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryByRefundNo_001() {
		Logger.start(true, "查询退款成功");
		try {
			PaymentDefaultWapper pdw=new PaymentDefaultWapper();
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("10");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			AcquireRefundBean ar = PaymentProxyCallService.AcquireRefund(aqb,aqb.getPayPrice());
			PaymentProxyCallService.approveRefund(ar.getRefundNo());
			Logger.comment("查询部分退款单号");
			PPPayment ppm = getPPPayment(aqb);
			RefundRequest mrr  = pdw.selectRefundRequestByPaymentId(ppm.getPaymentId()).get(0);
			
			List<String> refundNoList=new  ArrayList<>();
			refundNoList.add(mrr.getTraceId());
			QueryByRefundNoBean.setRefundNoList(refundNoList);
			QueryByRefundNoCall.setData(QueryByRefundNoBean);
			QueryByRefundNoCall.callService();
			Logger.verifyEquals("true", QueryByRefundNoCall.getString("IsSuccess"), "验证IsSuccess");
			
			JsonObject json = QueryByRefundNoCall.getJsonObject().get("Result").getAsJsonArray().get(0).getAsJsonObject();
			Logger.verifyEquals(mrr.getTradeNo(),json.get("TradeNo").getAsString(),"验证TradeNo");
			Logger.verifyEquals(mrr.getOrderId(),json.get("OrderId").getAsString(),"验证OrderId");
			Logger.verifyEquals(mrr.getAppId(),json.get("AppId").getAsString(),"验证AppId");
			Logger.verifyEquals(mrr.getPayType(),json.get("PayType").getAsString(),"验证PayType");
			Logger.verifyEquals(mrr.getRefundAmount().toString(),json.get("RefundAmount").getAsString(),"验证RefundAmount");
			Logger.verifyEquals(mrr.getCurrencyType(),json.get("CurrencyType").getAsString(),"验证CurrencyType");
			Logger.verifyEquals(mrr.getApproveStatus().toString(),json.get("ApproveStatus").getAsString(),"验证ApproveStatus");
			
			Logger.verifyEquals(YMTDateUtil.getFormatDateString(mrr.getApprovedTime()),json.get("ApprovedTime").getAsString(),"验证ApprovedTime");
			Logger.verifyEquals(YMTDateUtil.getFormatDateString(mrr.getCreatedTime()),json.get("CreatedTime").getAsString(),"验证CreatedTime");
			Logger.verifyEquals(mrr.getApprovedUser(),json.get("ApprovedUser").getAsString(),"验证ApprovedUser");
			Logger.verifyEquals(mrr.getRefundStatus().toString(),json.get("RefundStatus").getAsString(),"验证RefundStatus");
			
			Logger.verifyEquals(YMTDateUtil.getFormatDateString(mrr.getRefundTime()),json.get("RefundTime").getAsString(),"验证RefundTime");
			
			Logger.verifyEquals(mrr.getRefundBatchNo(),json.get("RefundBatchNo").getAsString(),"验证RefundBatchNo");
			Logger.verifyEquals(mrr.getPaymentId(),json.get("PaymentId").getAsString(),"验证PaymentId");
			Logger.verifyEquals(mrr.getTradeType().toString(),json.get("TradeType").getAsString(),"验证TradeType");
			Logger.verifyEquals(mrr.getSoftDeleteFlag(),json.get("IsIntercept").getAsBoolean(),"验证IsIntercept");
			
			Logger.verifyEquals(getChannelType(mrr.getPayType()).getCode(),json.get("PayChannel").getAsString(),"验证PayChannel");
//			TODO check data
//			select top 100 * from PP_PaymentCheck where uploadfile like '%2016-12-17%'
//			select top 100 * from PP_PaymentCheckDetail where PaymentCheckId = 41
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_QueryByRefundNo_002() {
		Logger.start(true, "不输入refundNoList,失败");
		try {
//			QueryByRefundNoBean.setDate("2016-12-16");
//			QueryByRefundNoBean.setNo("1");
			QueryByRefundNoCall.setData(QueryByRefundNoBean);
			QueryByRefundNoCall.callService();
			Logger.verifyIsContains("退款申请号不可为空", QueryByRefundNoCall.getString("ErrorMessage"), "验证返回ErrorMessage");
			Logger.verifyEquals("1000", QueryByRefundNoCall.getString("ErrorCode"), "验证返回ErrorCode");
			Logger.verifyEquals("false", QueryByRefundNoCall.getString("IsSuccess"), "验证返回IsSuccess");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryByRefundNo_003() {
		Logger.start(true, "渠道60查询退款成功");
		try {
			PaymentDefaultWapper pdw=new PaymentDefaultWapper();
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("60");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			AcquireRefundBean ar = PaymentProxyCallService.AcquireRefund(aqb,aqb.getPayPrice());
			PaymentProxyCallService.approveRefund(ar.getRefundNo());
			Logger.comment("查询部分退款单号");
			PPPayment ppm = getPPPayment(aqb);
			RefundRequest mrr  = pdw.selectRefundRequestByPaymentId(ppm.getPaymentId()).get(0);
			
			List<String> refundNoList=new  ArrayList<>();
			refundNoList.add(mrr.getTraceId());
			QueryByRefundNoBean.setRefundNoList(refundNoList);
			QueryByRefundNoCall.setData(QueryByRefundNoBean);
			QueryByRefundNoCall.callService();
			Logger.verifyEquals("true", QueryByRefundNoCall.getString("IsSuccess"), "验证IsSuccess");
			
			JsonObject json = QueryByRefundNoCall.getJsonObject().get("Result").getAsJsonArray().get(0).getAsJsonObject();
			Logger.verifyEquals(mrr.getTradeNo(),json.get("TradeNo").getAsString(),"验证TradeNo");
			Logger.verifyEquals(mrr.getOrderId(),json.get("OrderId").getAsString(),"验证OrderId");
			Logger.verifyEquals(mrr.getAppId(),json.get("AppId").getAsString(),"验证AppId");
			Logger.verifyEquals(mrr.getPayType(),json.get("PayType").getAsString(),"验证PayType");
			Logger.verifyEquals(mrr.getRefundAmount().toString(),json.get("RefundAmount").getAsString(),"验证RefundAmount");
			Logger.verifyEquals(mrr.getCurrencyType(),json.get("CurrencyType").getAsString(),"验证CurrencyType");
			Logger.verifyEquals(mrr.getApproveStatus().toString(),json.get("ApproveStatus").getAsString(),"验证ApproveStatus");
			
			Logger.verifyEquals(YMTDateUtil.getFormatDateString(mrr.getApprovedTime()),json.get("ApprovedTime").getAsString(),"验证ApprovedTime");
			Logger.verifyEquals(YMTDateUtil.getFormatDateString(mrr.getCreatedTime()),json.get("CreatedTime").getAsString(),"验证CreatedTime");
			Logger.verifyEquals(mrr.getApprovedUser(),json.get("ApprovedUser").getAsString(),"验证ApprovedUser");
			Logger.verifyEquals(mrr.getRefundStatus().toString(),json.get("RefundStatus").getAsString(),"验证RefundStatus");
			
			Logger.verifyEquals(YMTDateUtil.getFormatDateString(mrr.getRefundTime()),json.get("RefundTime").getAsString(),"验证RefundTime");
			
			Logger.verifyEquals(mrr.getRefundBatchNo(),json.get("RefundBatchNo").getAsString(),"验证RefundBatchNo");
			Logger.verifyEquals(mrr.getPaymentId(),json.get("PaymentId").getAsString(),"验证PaymentId");
			Logger.verifyEquals(mrr.getTradeType().toString(),json.get("TradeType").getAsString(),"验证TradeType");
			Logger.verifyEquals(mrr.getSoftDeleteFlag(),json.get("IsIntercept").getAsBoolean(),"验证IsIntercept");
			
			Logger.verifyEquals(getChannelType(mrr.getPayType()).getCode(),json.get("PayChannel").getAsString(),"验证PayChannel");
//			TODO check data
//			select top 100 * from PP_PaymentCheck where uploadfile like '%2016-12-17%'
//			select top 100 * from PP_PaymentCheckDetail where PaymentCheckId = 41
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryByRefundNo_004() {
		Logger.start(true, "查询70退款成功JIRACONN-18383宝付直接退余额");
		try {
			PaymentDefaultWapper pdw=new PaymentDefaultWapper();
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("70");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			AcquireRefundBean ar = PaymentProxyCallService.AcquireRefund(aqb,aqb.getPayPrice());
			PaymentProxyCallService.approveRefund(ar.getRefundNo());
			Logger.comment("查询部分退款单号");
			PPPayment ppm = getPPPayment(aqb);
			RefundRequest mrr  = pdw.selectRefundRequestByPaymentId(ppm.getPaymentId()).get(0);
			
			List<String> refundNoList=new  ArrayList<>();
			refundNoList.add(mrr.getTraceId());
			QueryByRefundNoBean.setRefundNoList(refundNoList);
			QueryByRefundNoCall.setData(QueryByRefundNoBean);
			QueryByRefundNoCall.callService();
			Logger.verifyEquals("true", QueryByRefundNoCall.getString("IsSuccess"), "验证IsSuccess");
			
			JsonObject json = QueryByRefundNoCall.getJsonObject().get("Result").getAsJsonArray().get(0).getAsJsonObject();
			
			Logger.verifyEquals(mrr.getTradeNo(),json.get("TradeNo").getAsString(),"验证TradeNo");
			Logger.verifyEquals(mrr.getOrderId(),json.get("OrderId").getAsString(),"验证OrderId");
			Logger.verifyEquals(mrr.getAppId(),json.get("AppId").getAsString(),"验证AppId");
			Logger.verifyEquals(mrr.getPayType(),json.get("PayType").getAsString(),"验证PayType");
			Logger.verifyEquals(mrr.getRefundAmount().toString(),json.get("RefundAmount").getAsString(),"验证RefundAmount");
			Logger.verifyEquals(mrr.getCurrencyType(),json.get("CurrencyType").getAsString(),"验证CurrencyType");
			Logger.verifyEquals(mrr.getApproveStatus().toString(),json.get("ApproveStatus").getAsString(),"验证ApproveStatus");
			
			Logger.verifyEquals(YMTDateUtil.getFormatDateString(mrr.getApprovedTime()),json.get("ApprovedTime").getAsString(),"验证ApprovedTime");
			Logger.verifyEquals(YMTDateUtil.getFormatDateString(mrr.getCreatedTime()),json.get("CreatedTime").getAsString(),"验证CreatedTime");
			Logger.verifyEquals(mrr.getApprovedUser(),json.get("ApprovedUser").getAsString(),"验证ApprovedUser");
			Logger.verifyEquals(mrr.getRefundStatus().toString(),json.get("RefundStatus").getAsString(),"验证RefundStatus");
			
//			Logger.verifyEquals(YMTDateUtil.getFormatDateString(mrr.getRefundTime()),json.get("RefundTime").getAsString(),"验证RefundTime");
			
			Logger.verifyEquals(mrr.getRefundBatchNo(),json.get("RefundBatchNo").getAsString(),"验证RefundBatchNo");
			Logger.verifyEquals(mrr.getPaymentId(),json.get("PaymentId").getAsString(),"验证PaymentId");
			Logger.verifyEquals(mrr.getTradeType().toString(),json.get("TradeType").getAsString(),"验证TradeType");
			Logger.verifyEquals(mrr.getSoftDeleteFlag(),json.get("IsIntercept").getAsBoolean(),"验证IsIntercept");
			
			Logger.verifyEquals(getChannelType(mrr.getPayType()).getCode(),json.get("PayChannel").getAsString(),"验证PayChannel");
			Logger.debug(FormatUtil.beanToGSONString(mrr));
//			TODO check data
//			select top 100 * from PP_PaymentCheck where uploadfile like '%2016-12-17%'
//			select top 100 * from PP_PaymentCheckDetail where PaymentCheckId = 41
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryByRefundNo_005() {
		Logger.start(true, "查询9花呗退款成功");
		try {
			PaymentDefaultWapper pdw=new PaymentDefaultWapper();
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("9");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			AcquireRefundBean ar = PaymentProxyCallService.AcquireRefund(aqb,aqb.getPayPrice());
			PaymentProxyCallService.approveRefund(ar.getRefundNo());
			Logger.comment("查询部分退款单号");
			PPPayment ppm = getPPPayment(aqb);
			RefundRequest mrr  = pdw.selectRefundRequestByPaymentId(ppm.getPaymentId()).get(0);
			
			List<String> refundNoList=new  ArrayList<>();
			refundNoList.add(mrr.getTraceId());
			QueryByRefundNoBean.setRefundNoList(refundNoList);
			QueryByRefundNoCall.setData(QueryByRefundNoBean);
			QueryByRefundNoCall.callService();
			Logger.verifyEquals("true", QueryByRefundNoCall.getString("IsSuccess"), "验证IsSuccess");
			
			JsonObject json = QueryByRefundNoCall.getJsonObject().get("Result").getAsJsonArray().get(0).getAsJsonObject();
			
			Logger.verifyEquals(mrr.getTradeNo(),json.get("TradeNo").getAsString(),"验证TradeNo");
			Logger.verifyEquals(mrr.getOrderId(),json.get("OrderId").getAsString(),"验证OrderId");
			Logger.verifyEquals(mrr.getAppId(),json.get("AppId").getAsString(),"验证AppId");
			Logger.verifyEquals(mrr.getPayType(),json.get("PayType").getAsString(),"验证PayType");
			Logger.verifyEquals(mrr.getRefundAmount().toString(),json.get("RefundAmount").getAsString(),"验证RefundAmount");
			Logger.verifyEquals(mrr.getCurrencyType(),json.get("CurrencyType").getAsString(),"验证CurrencyType");
			Logger.verifyEquals(mrr.getApproveStatus().toString(),json.get("ApproveStatus").getAsString(),"验证ApproveStatus");
			
			Logger.verifyEquals(YMTDateUtil.getFormatDateString(mrr.getApprovedTime()),json.get("ApprovedTime").getAsString(),"验证ApprovedTime");
			Logger.verifyEquals(YMTDateUtil.getFormatDateString(mrr.getCreatedTime()),json.get("CreatedTime").getAsString(),"验证CreatedTime");
			Logger.verifyEquals(mrr.getApprovedUser(),json.get("ApprovedUser").getAsString(),"验证ApprovedUser");
			Logger.verifyEquals(mrr.getRefundStatus().toString(),json.get("RefundStatus").getAsString(),"验证RefundStatus");
			
			Logger.verifyEquals(YMTDateUtil.getFormatDateString(mrr.getRefundTime()),json.get("RefundTime").getAsString(),"验证RefundTime");
			
			Logger.verifyEquals(mrr.getRefundBatchNo(),json.get("RefundBatchNo").getAsString(),"验证RefundBatchNo");
			Logger.verifyEquals(mrr.getPaymentId(),json.get("PaymentId").getAsString(),"验证PaymentId");
			Logger.verifyEquals(mrr.getTradeType().toString(),json.get("TradeType").getAsString(),"验证TradeType");
			Logger.verifyEquals(mrr.getSoftDeleteFlag(),json.get("IsIntercept").getAsBoolean(),"验证IsIntercept");
			
			Logger.verifyEquals(getChannelType(mrr.getPayType()).getCode(),json.get("PayChannel").getAsString(),"验证PayChannel");
			Logger.debug(FormatUtil.beanToGSONString(mrr));
//			TODO check data
//			select top 100 * from PP_PaymentCheck where uploadfile like '%2016-12-17%'
//			select top 100 * from PP_PaymentCheckDetail where PaymentCheckId = 41
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
    public  ChannelTypeEnum getChannelType(String payType) {
        if (payType.equals(PayTypeEnum.AliPayApp.getCode()) || payType.equals(PayTypeEnum.AliPayPc.getCode())
                || payType.equals(PayTypeEnum.AliPayWap.getCode() )|| payType.equals(PayTypeEnum.AliPayHbfq.getCode())) {
            return ChannelTypeEnum.AliPay;
        } else if (payType.equals(PayTypeEnum.CmbApp.getCode())) {
            return ChannelTypeEnum.CmbPay;
        } else if (payType.equals(PayTypeEnum.ApplePay.getCode()) ||payType.equals(PayTypeEnum.ApplePay2.getCode()) ) {
            return ChannelTypeEnum.ApplePay;
        } else if (payType.equals(PayTypeEnum.BaofooPay.getCode())) {
            return ChannelTypeEnum.BaofooPay;
        } else {
            return ChannelTypeEnum.WeiXinPay;
        }
    }
	private PPPayment getPPPayment(AcquireOrderBean aqb){
		PaymentDefaultWapper pdw=new PaymentDefaultWapper();
		PPPayment ppm =new PPPayment();
		PPBussinessOrder ppb;
		try {
			ppb = pdw.selectPPBussinessorderByTraceId(aqb.getTraceId()).get(0);
			String businessoid = ppb.getBussinessOrderId();
			ppm = pdw.selectPPPaymentByBussinessOrderId(businessoid).get(0);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return ppm;
	}
}